package cn.uppower.wordaliy.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.ActivityUtils;

import org.litepal.LitePal;

import cn.uppower.wordaliy.R;
import cn.uppower.wordaliy.model.UserInfo;
import cn.uppower.wordaliy.WordaliyApplication;
import cn.uppower.wordaliy.util.MD5Utils;
import cn.uppower.wordaliy.util.PhoneFormatCheckUtils;
import cn.uppower.wordaliy.widget.BaseActivity;

public class LoginActivity extends BaseActivity {

    private EditText et_pwd,et_phone;
    private TextView tv_forget_pwd,tv_title,tv_register,tv_toggle_jzmm;
    private ImageView iv_goback,iv_weibo_login,iv_wechat_login,iv_qq_login;
    private Button btn_login;
    private RadioButton rb_remenber_me;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public Integer getLayoutResId() {
        return  R.layout.activity_login;
    }

    @Override
    public void init() {

        tv_forget_pwd = findViewById(R.id.tv_forget_pwd);
        et_pwd = findViewById(R.id.et_pwd);
        et_phone = findViewById(R.id.et_phone);
        iv_goback = findViewById(R.id.iv_goback);
        tv_title = findViewById(R.id.tv_title);
        tv_register = findViewById(R.id.tv_register);
        iv_weibo_login = findViewById(R.id.iv_weibo_login);
        iv_wechat_login = findViewById(R.id.iv_wechat_login);
        iv_qq_login = findViewById(R.id.iv_qq_login);
        btn_login = findViewById(R.id.btn_login);
        rb_remenber_me = findViewById(R.id.rb_remenber_me);
        tv_title.setText("Wordaliy");
        tv_toggle_jzmm = findViewById(R.id.tv_toggle_jzmm);
        tv_toggle_jzmm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                rb_remenber_me.setChecked(!rb_remenber_me.isChecked());
            }
        });

//        注册
        tv_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(LoginActivity.this,RegisterActivity.class));
            }
        });
//        设置返回按钮不显示
        iv_goback.setVisibility(View.GONE);
//        登录监听
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String phone=et_phone.getText().toString().trim();
                String pwd=et_pwd.getText().toString().trim();

                if (phone.isEmpty()){
                    et_phone.setError("手机号不能为空");
                    return;
                }

                if (!PhoneFormatCheckUtils.isPhoneLegal(phone)){
                    et_phone.setError("手机号不合法");
                    return;
                }
//                取消错误提示
                et_phone.setError(null);

                if (pwd.isEmpty()){
                    et_pwd.setError("密码不能为空");
                    return;
                }
                et_pwd.setError(null);
//                进行登录操作 成功就本地存储账号密码并进入主页面，失败提示密码错误
                UserInfo userInfo=LitePal.where("userPhone = ?",phone).findFirst(UserInfo.class);
                if (userInfo==null){
                    Toast.makeText(LoginActivity.this, "不存在该用户", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!userInfo.getPwd().equals(MD5Utils.MD5Encode(pwd,null))){
                    Toast.makeText(LoginActivity.this, "账号或密码错误！", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (rb_remenber_me.isChecked()){
//                   存储密码
                    WordaliyApplication.wordaliyEditor.putString("userPhone",userInfo.getUserPhone());
                    WordaliyApplication.wordaliyEditor.putString("registeTime",userInfo.getRegisteTime());
                    WordaliyApplication.wordaliyEditor.commit();

                }
                WordaliyApplication.userInfo = userInfo;
                startActivity(new Intent(LoginActivity.this,MainActivity.class));
            }
        });

        iv_qq_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(LoginActivity.this, "QQ登录", Toast.LENGTH_SHORT).show();
            }
        });

        iv_wechat_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(LoginActivity.this, "微信登录", Toast.LENGTH_SHORT).show();
            }
        });

        iv_weibo_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(LoginActivity.this, "微博登录", Toast.LENGTH_SHORT).show();
            }
        });
        tv_forget_pwd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(LoginActivity.this, "忘记密码", Toast.LENGTH_SHORT).show();
            }
        });




    }

    @Override
    protected void onResume() {
        super.onResume();
//        删除之前的页面，主要用户主界面点击退出登录之后返回该界面，并移除其他界面
        ActivityUtils.finishAllActivitiesExceptNewest();
    }
}
